/**
 * Array.from()
 *  静态方法从可迭代或类数组对象创建一个新的浅拷贝的数组实例
 */

/**
 * 1. from string
 */
console.log(Array.from('foo'));

/**
 * 2. from array
 */
console.log(Array.from([1, 2, 3]);

/**
 * 3. from set
 */
const set = new Set(["foo", "bar", "baz", "foo"]);
console.log(Array.from(set))

/**
 * 4. from map
 */
const map = new Map([
    [1, 2],
    [2, 4],
    [4, 8],
]);
console.log(Array.from(map))
// [[1, 2], [2, 4], [4, 8]]

/**
 * 5. from map values
 */
const mapper = new Map([
    ["1", "a"],
    ["2", "b"],
]);
console.log(Array.from(mapper.values()))
// ['a', 'b'];

/**
 * 6. from arguments
 * @returns 
 */
function f() {
    return Array.from(arguments);
}
console.log(f(1, 2, 3))
// [ 1, 2, 3 ]


/**
 * 7. from 类数组
 */
// 类数组：有length属性和索引元素的对象
let arr7 = Array.from({ length: 5 }, (v, i) => i);
console.log(arr7)

/**
 * 8. 应用：序列生成器
 * 序列生成器函数（通常称为“range”，例如 Clojure、PHP 等）
 * @param {*} start 
 * @param {*} stop 
 * @param {*} step 
 * @returns 
 */
const range = (start, stop, step) =>
  Array.from({ length: (stop - start) / step + 1 }, (_, i) => start + i * step);

// 生成的数字范围 0..4
range(0, 4, 1);
// [0, 1, 2, 3, 4]

// 生成的数字范围 1..10，步长为 2
range(1, 10, 2);
// [1, 3, 5, 7, 9]

// 使用 Array.from 生成字母表，并将其序列排序
range("A".charCodeAt(0), "Z".charCodeAt(0), 1).map((x) =>
  String.fromCharCode(x),
);
// ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]